#!/usr/local/bin/python
# -*- coding: utf-8 -*-



from data_alchemy.account_book_alchemy import AccountBookAlchemy
from data_alchemy.balance_sheet_alchemy import BalanceSheetAlchemy
import initBalanceSheetPage
from data_alchemy.balance_sheet_monthly_alchemy import BalanceSheetMonthlyAlchemy


class BalanceSheet:

    def __init__(self, year=2022, month=1):
        '''
        函数功能：
        '''

        self.year = year
        self.month = month
        self.accountBookDetail = {}     #暂时未使用
        self.propertyIncrease = {}      #当月各科目资产增加情况
        self.propertyDecrease = {}      #当月各科目资产减少情况




    def AddBalanceSheetGroupInfor(self, year, month, subjectName, subjectPath):
        '''
        函数功能：获取特定的账簿项信息,并生成资产负债表数据
        :return:
        '''
        accountBookAlchemy = AccountBookAlchemy()
        result = accountBookAlchemy.SearchAccountBookByMonthSub(year, month, subjectPath)

        if len(result)==0:
            return 0.0, 0.0
        startValue = 0.0
        endValue = 0.0
        category = ""
        for i in range(len(result)):
            dataRow = result[i]
            startValue = startValue + dataRow.start_value
            endValue = endValue + dataRow.end_value
            dataType = dataRow.account_type


        #logger.debug("subjectNam=%s; dataType=%s\n"%(subjectName, dataType))
        balanceSheetAlchemy = BalanceSheetAlchemy(year=year,
                                                  month=month,
                                                  category=subjectName,
                                                  dataType=dataType ,
                                                  path=subjectPath,
                                                  startValue=startValue,
                                                  endValue=endValue
                                                  )

        balanceSheetAlchemy.AddBalanceSheetItem()

        return startValue, endValue


    def genBalanceSheet(self):
        '''
        函数功能：生成资产负债表
        :return:
        '''

        balanceSheetAlchemy = BalanceSheetAlchemy()
        balanceSheetAlchemy.DelBalanceSheetByMonth(self.year, self.month)

        balanceSheetMonthlyAlchemy = BalanceSheetMonthlyAlchemy()
        balanceSheetMonthlyAlchemy.DelBalanceSheetByMonth(self.year, self.month)

        propertyStartValueTotal=0.0
        propertyEndValueTotal=0.0
        ownershipStartValueTotal=0.0
        ownershipEndValueTotal=0.0

        for subjectName in initBalanceSheetPage.BALANCE_SHEET_PROPERTY_LIST:
            subjectPath = initBalanceSheetPage.BALANCE_SHEET_PROPERTY_LIST[subjectName]
            startValue, endValue = self.AddBalanceSheetGroupInfor(self.year, self.month, subjectName, subjectPath)
            propertyStartValueTotal = propertyStartValueTotal + startValue
            propertyEndValueTotal = propertyEndValueTotal + endValue


        for subjectName in initBalanceSheetPage.BALANCE_SHEET_OWNERSHIP_LIST:
            subjectPath = initBalanceSheetPage.BALANCE_SHEET_OWNERSHIP_LIST[subjectName]
            startValue, endValue = self.AddBalanceSheetGroupInfor(self.year, self.month, subjectName, subjectPath)
            ownershipStartValueTotal = ownershipStartValueTotal + startValue
            ownershipEndValueTotal = ownershipEndValueTotal + endValue

        propertyBalanceSheetMonthlyAlchemy = BalanceSheetMonthlyAlchemy(year=self.year,
                                                                month=self.month,
                                                                dataType="property",
                                                                startValue=propertyStartValueTotal,
                                                                endValue=propertyEndValueTotal
                                                                )
        propertyBalanceSheetMonthlyAlchemy.AddBalanceSheetItem()

        ownershipBalanceSheetMonthlyAlchemy = BalanceSheetMonthlyAlchemy(year=self.year,
                                                                month=self.month,
                                                                dataType="ownership",
                                                                startValue=ownershipStartValueTotal,
                                                                endValue=ownershipEndValueTotal
                                                                )
        ownershipBalanceSheetMonthlyAlchemy.AddBalanceSheetItem()



    def getBalanceSheetDataMonthly(self, dataType):
        '''
        函数功能：获取指定月份资产负债表的数据
        :param year:
        :param month:
        :param dataType: 标记资产或是权属
        :return:
        '''

        balanceSheetAlchemy = BalanceSheetAlchemy()
        balanceSheetResult = balanceSheetAlchemy.SearchBalanceSheetByMonthDataType(self.year, self.month, dataType)

        return balanceSheetResult



def main():

    blanceSheet = BalanceSheet(2022, 7)
    blanceSheet.genBalanceSheet()


if __name__ == '__main__':
    main()